package com.eastcom.mystope.commandline;

import java.net.InetSocketAddress;
import java.util.Scanner;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.future.CloseFuture;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.eastcom.mystope.daemon.core.app.RuntimeContext;
public class SocketClient extends Thread {

	private static Log logger = LogFactory.getLog(SocketClient.class);

	private NioSocketConnector connector;

	private RuntimeContext context;
	private String destAddress;

	private IoSession session;

	private boolean ready = false;
	private String destPort;

	private boolean stopFlag = false;

	private int retryInterval;

	public SocketClient() {
	}

	public void run() {

		if (!connect()) {
			logger.debug("error!");
			return;
		}
		Scanner scanner = new Scanner(System.in);
		while (!stopFlag) {
			String input = scanner.next();
			if(input.trim().equalsIgnoreCase("stop")){
				stopFlag = true;
				
			}
			session.write(input);
		}
		disconnect();
	}


	private boolean connect() {
		try {
			if (ready)
				return true;
			if (session != null && session.isConnected()) {
				logger.debug("begin disconnect to " + getDestAddress() + ":"
						+ getDestPort());
				disconnect();
			}
			logger.debug("begin connect to " + getDestAddress() + ":"
					+ getDestPort());
			ConnectFuture cf = connector.connect(new InetSocketAddress(
					getDestAddress(),Integer.valueOf(getDestPort())));
			cf.awaitUninterruptibly();

			session = cf.getSession();
			logger.debug("dsfdsfds");
		} catch (Exception e) {
			logger.debug(e.getMessage());
			setReady(false);
			return false;
		}
		if (session != null) {
			setReady(true);
			logger.debug("ggggg");
			return true;
		}
		return false;
	}

	public void disconnect() {
		logger.debug("Client disconnect started...");
		setReady(false);
		try {
			if (session.isConnected()) {
				logger
				.debug("Client disconnect started...session.isConnected():"
						+ session.isConnected());
			}
			if (session != null && session.isConnected()) {

				logger.debug("Client disconnect started...1");
				CloseFuture future = session.close(true);
				logger.debug("Client disconnect started...future2:" + future);
				future.awaitUninterruptibly(retryInterval * 1000);
				logger.debug("Client disconnect started...future3:" + future);

				logger.debug("Client disconnect finished");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("disconnect error" + e.getMessage(), e);
		}
	}

	public boolean isReady() {
		return ready;
	}

	public void sendMessage(MessageObject msg) {
		if (session != null)
			session.write(msg);
	}

	public void setConnector(NioSocketConnector connector) {
		this.connector = connector;
	}

	public NioSocketConnector getConnector() {
		return connector;
	}

	public void setDestAddress(String destAddress) {
		this.destAddress = destAddress;
	}

	public String getDestAddress() {
		return destAddress;
	}

	public void setDestPort(String destPort) {
		this.destPort = destPort;
	}

	public String getDestPort() {
		return destPort;
	}

	public void setReady(boolean ready) {
		this.ready = ready;
	}

	public void setContext(RuntimeContext context) {
		this.context = context;
	}

	public RuntimeContext getContext() {
		return context;
	}

	public void setRetryInterval(int retryInterval) {
		this.retryInterval = retryInterval;
	}

	public int getRetryInterval() {
		return retryInterval;
	}

}
